package com.glory.hikvisionmobile.fragment;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.activity.SettingActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import static com.glory.hikvisionmobile.utils.MyUtils.setPicToDrawable;

public class PDAWorkFragment extends FreeUI_BaseFragment {


    @BindView(R.id.tvTtitle)
    TextView tvTtitle;
    @BindView(R.id.rvList)
    RecyclerView rvList;

    public PDAWorkFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_all;
    }

    @Override
    protected void initView() {
        tvTtitle.setText("作业管理(" + Constant.CURRENT_USER + ")");
        rvList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }

    @Override
    protected void initData() {
        try {
            initArea();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initArea() throws IOException {
        InputStream workJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("work.txt");
        String workJson = FreeApi_StringUtils.getString(workJsonStream);
        List<FreeUI_TabIndexGridEntity> entity = filteringData(new Gson().fromJson(workJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(),entity), R.layout.free_ui_glory_adapter_tab_index_grid);
        tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        tabIndexGridAdapter.isFirstOnly(true);
        rvList.setAdapter(tabIndexGridAdapter);
    }

    private List<FreeUI_TabIndexGridEntity> filteringData (List<FreeUI_TabIndexGridEntity> entity)
    {
        Iterator it = entity.iterator();
        while (it.hasNext())
        {
            FreeUI_TabIndexGridEntity item = (FreeUI_TabIndexGridEntity) it.next();
            try {
                Class<?> aClass = Class.forName(item.getSkipClass());
                String name = aClass.getSimpleName();
                if(!Constant.Jurisdiction.get("PDAWorkFragment").containsKey(name))
                {

                    it.remove();

                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden)
            Constant.CURRENTFRAGMENTNAME =  "PDAWorkFragment";
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick(R.id.ivSetting)
    public void onViewClicked() {
        FreeApi_Utils.skipWithExtraIntent("header", getActivity(), SettingActivity.class);
    }
}
